package pt.um.jspecput.lib.writer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import pt.um.jspecput.util.Constants;

/**
 * The Class FileHandler server as an utility class to handle file operations.
 */
public abstract class FileHandler {

    /**
     * Creates a file.
     * 
     * @param file
     *            the file to create
     * @return true, if successful
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static boolean createFile(File file) throws IOException {
        return file.createNewFile();
    }

    /**
     * Creates a file.
     * 
     * @param fileName
     *            the name of the file
     * @return true, if successful
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static boolean createFile(String fileName) throws IOException {
        File f = new File(fileName);
        return FileHandler.createFile(f);
    }

    /**
     * Deletes a file.
     * 
     * @param file
     *            the file to delete
     * @return true, if successful
     */
    public static boolean deleteFile(File file) {
        return file.delete();
    }

    /**
     * Deletes a file.
     * 
     * @param fileName
     *            the name of the file to delete
     * @return true, if successful
     */
    public static boolean deleteFile(String fileName) {
        File f = new File(fileName);
        return FileHandler.deleteFile(f);
    }

    /**
     * Opens a file writer on a java file. If the file does not exist it will
     * create it.
     * 
     * @param fileName
     *            the name of the file
     * @return the writer
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Writer openFileWriter(String fileName) throws IOException {
        return FileHandler.openFileWriter(fileName,
                Constants.JAVA_FILE_EXTENSION);
    }

    /**
     * Opens a buffered file writer for the given file. If the file does not
     * exist it will create it.
     * 
     * @param fileName
     *            the name of the file to open
     * @param fileExtension
     *            the extension of the file to open
     * @return the writer
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Writer openFileWriter(String fileName, String fileExtension)
            throws IOException {
        File f = new File(fileName + "." + fileExtension);

        return FileHandler.openFileWriter(f);
    }

    /**
     * Opens a buffered file writer for the given file. If the file does not
     * exist it will create it.
     * 
     * @param file
     *            the file
     * @return the writer
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Writer openFileWriter(File file) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter writer = new BufferedWriter(fileWriter);

        return writer;
    }

    /**
     * Closes the given file writer.
     * 
     * @param writer
     *            the writer
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void closeFileWriter(Writer writer) throws IOException {
        writer.close();
    }

    /**
     * Opens a file reader on a java file.
     * 
     * @param fileName
     *            the file name
     * @return the reader
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Reader openFileReader(String fileName) throws IOException {
        return FileHandler.openFileReader(fileName,
                Constants.JAVA_FILE_EXTENSION);
    }

    /**
     * Opens a file reader on the given file.
     * 
     * @param fileName
     *            the name of the file to open
     * @param fileExtension
     *            the extension of the file to open
     * @return the reader
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Reader openFileReader(String fileName, String fileExtension)
            throws IOException {
        File f = new File(fileName + "." + fileExtension);

        return FileHandler.openFileReader(f);
    }

    /**
     * Opens a file reader on the given file.
     * 
     * @param file
     *            the file
     * @return the reader
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Reader openFileReader(File file) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }

        FileReader fileReader = new FileReader(file);
        BufferedReader reader = new BufferedReader(fileReader);

        return reader;
    }

    /**
     * Closes a file reader.
     * 
     * @param reader
     *            the reader
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void closeFileReader(Reader reader) throws IOException {
        reader.close();
    }
}
